package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.domain.ScenicspotComment;
import cn.wolfcode.luowowo.comment.domain.ScenicspotReply;
import cn.wolfcode.luowowo.comment.query.ScenicspotCommentQuery;
import cn.wolfcode.luowowo.comment.repository.IScenicspotCommentRepository;
import cn.wolfcode.luowowo.comment.service.IScenicspotCommentService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
public class ScenicspotCommentServiceImpl implements IScenicspotCommentService {
    @Autowired
    private IScenicspotCommentRepository repository;
    @Autowired
    private MongoTemplate template;

    public ScenicspotComment saveOrUpdate(ScenicspotComment scenicspotComment,ScenicspotReply reply) {
        if(StringUtils.hasLength(scenicspotComment.getId()) && reply != null){
            scenicspotComment = repository.findById(scenicspotComment.getId()).get();
            //设置关联的回复
            reply.setCreateTime(new Date());
            List<ScenicspotReply> replyList = scenicspotComment.getReply();
            replyList.add(reply);
            scenicspotComment.setReply(replyList);
        }
        //设置评论创建时间
        if(!StringUtils.hasLength(scenicspotComment.getId())){
            scenicspotComment.setCreateTime(new Date());
            //设置是否有图
            if(scenicspotComment.getImages() != null){
                scenicspotComment.setHasImg(true);
            }
        }
        repository.save(scenicspotComment);
        return scenicspotComment;
    }

    public List<ScenicspotComment> listAll() {
        return repository.findAll(Sort.by(Sort.Direction.ASC,"createTime"));
    }

    public List<ScenicspotComment> getCreateTimeTop10() {
        Query query = new Query();
        query.with(Sort.by(Sort.Direction.ASC,"createTime")).limit(10);
        return template.find(query, ScenicspotComment.class);
    }

    public Map<String, Integer> getStatisNum() {
        Map<String, Integer> map = new HashMap<>();
        List<ScenicspotComment> hasImg=  repository.findByHasImg(true);
        map.put("hasImg", hasImg.size());
        List<ScenicspotComment> comments = repository.findAll();
        Integer goodComment = 0;
        Integer middleComment = 0;
        Integer badComment = 0;
        for (ScenicspotComment comment : comments) {
            if(comment.getCommentType() == ScenicspotComment.GOOD_COMMENTS){
                goodComment += 1;
            }else if (comment.getCommentType() == ScenicspotComment.MIDDLE_COMMENTS){
                middleComment += 1;
            }else {
                badComment += 1;
            }
        }
        map.put("goodComment",goodComment);
        map.put("middleComment",middleComment);
        map.put("badComment",badComment);
        map.put("landmark",repository.findByCommentLike("建筑").size());
        map.put("manyPeople",repository.findByCommentLike("人很多").size());
        map.put("worth",repository.findByCommentLike("值得").size());
        map.put("fearHigh",repository.findByCommentLike("恐高").size());
        map.put("easyTraffic",repository.findByCommentLike("交通").size());
        map.put("fineView",repository.findByCommentLike("风景").size());
        return map;
    }

    public int addThumbNum(String commentId, int i) {
        ScenicspotComment comment = repository.findById(commentId).get();
        if(comment != null){
            comment.setThumbNum(comment.getThumbNum() + i);
        }
        ScenicspotComment save = repository.save(comment);
        return save.getThumbNum();
    }


    public Page query(ScenicspotCommentQuery qo) {
        //查询条件
        Criteria criteria = null;
        Query query = new Query();
        if(qo.getScenicspotId() != -1){
            //限制条件
            criteria = Criteria.where("scenicspotId").is(qo.getScenicspotId());
            query.addCriteria(criteria);
        }
        if(qo.getHasImg()){
            criteria = criteria.where("hasImg").is(qo.getHasImg());
            query.addCriteria(criteria);
        }else if(qo.getCommentType() == 0){
            criteria = criteria.where("totalScore").in(Arrays.asList(4,5));
            query.addCriteria(criteria);
        }else if(qo.getCommentType() == 1){
            criteria = criteria.where("totalScore").in(Arrays.asList(2,3));
            query.addCriteria(criteria);
        }else if(qo.getCommentType() == 2){
            criteria = criteria.where("totalScore").lt(2);
            query.addCriteria(criteria);
        }else if(qo.getKeyword() != null){
            criteria = criteria.where("comment").regex(".*?\\" +qo.getKeyword()+ ".*");
            query.addCriteria(criteria);
        }

        //查询总条数
        long count = template.count(query, ScenicspotComment.class);
        if(count == 0){
            return Page.empty();
        }
        //分页查询对象
        PageRequest pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.by(Sort.Direction.DESC, "createTime"));
        query.with(pageable);
        //查询符合条件的数据
        List<ScenicspotComment> list = template.find(query, ScenicspotComment.class);
        return new PageImpl(list,pageable,count);
    }


    @Override
    public Page queryByUserId(ScenicspotCommentQuery qo) {
        Query query =  new Query();
        if(qo.getUserId() != -1){
            Criteria criteria = Criteria.where("userId").is(qo.getUserId());
            query.addCriteria(criteria);
        }
        long count = template.count(query,ScenicspotComment.class);
        //查询总条数
        if(count == 0){
            return Page.empty();
        }
        //分页查询对象
        PageRequest pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.by(Sort.Direction.DESC, "createTime"));
        query.with(pageable);
        //查询
        List<ScenicspotComment> list = template.find(query, ScenicspotComment.class);
        return new PageImpl(list,pageable,count);
    }

    @Override
    public List<ScenicspotComment> queryTop3(Long userId) {
        Query query =  new Query();
        if(userId != -1){
            Criteria criteria = Criteria.where("userId").is(userId);
            query.addCriteria(criteria);
        }
        long count = template.count(query,ScenicspotComment.class);
        //查询总条数
        if(count == 0){
            return new ArrayList<>();
        }
        query.limit(2);
        query.with(new Sort(Sort.Direction.DESC,"id"));
        //查询
        return template.find(query, ScenicspotComment.class);
    }

    @Override
    public List<ScenicspotComment> listAllByUserId(Long id) {
        return repository.findByUserId(id);
    }
}
